package edu.sctu.practice.class02.hujie.online_classes_practice.imooc.Java_XMLRead.DOM4J;

import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.jdom.output.Format;

import java.awt.print.Book;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by hp on 2016/3/10.
 */
public class DOM4Jtest {
    public static ArrayList<Book> bookList=new ArrayList<>();
    private void parseXML(){
        //解析books.xml文件
        //创建SAXReader对象reader
        SAXReader reader = new SAXReader();
        try {
            //通过reader对象的read方法加载book.xml文件
            Document document = reader.read(new File("src\\main\\java\\edu\\sctu\\practice\\class02\\hujie\\online_classes_practice\\imooc\\Java_XMLRead\\books.xml"));
            //通过document对象获取节点bookstore
            Element bookStore = document.getRootElement();
            //通过element对象的elementIterator方法获取迭代器
            Iterator it = bookStore.elementIterator();
            //遍历迭代器，获取根节点中的信息(书籍)
            while (it.hasNext()){
                System.out.println("********开始遍历某一本书********");
                Element book=(Element)it.next();
                //获取book的属性名以及属性值
                List<Attribute> bookAttrs= book.attributes();
                for (Attribute attr:bookAttrs){
                    System.out.println("属性名："+attr.getName()+"属性值："+attr.getValue());
                }
                Iterator itt=book.elementIterator();
                while (itt.hasNext()){
                    Element bookChild=(Element)itt.next();
                    System.out.println("节点名："+bookChild.getName()+"--节点值："+bookChild.getStringValue());
                }
                System.out.println("********结束遍历某一本书********");
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
    public void createXML(){
        //1.创建document对象，代表整个xml文档
        Document document= DocumentHelper.createDocument();
        //2.创建根节点
        Element rss=document.addElement("rss");
        //3.向rss节点中添加version属性
        rss.addAttribute("version","2.0");
        //4.生成子节点及节点内容
        Element channel=rss.addElement("channel");
        Element title=channel.addElement("title");
        title.setText("国内最新新闻");
        //5.设置xml的格式 默认UTF-8
        OutputFormat format=OutputFormat.createPrettyPrint();
//        format.setEncoding("GBK");
        //6.生成xml文件
        File file=new File("src\\main\\java\\edu\\sctu\\practice\\class02\\hujie\\online_classes_practice\\imooc\\Java_XMLRead\\DOM4J\\rssnews.xml");
        XMLWriter writer;
        try {
            writer=new XMLWriter(new FileOutputStream(file),format);
            //设置是否转义默认true，代表转义
            writer.setEscapeText(false);
            writer.write(document);
            writer.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        DOM4Jtest test=new DOM4Jtest();
        test.createXML();
    }
}
